Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, I will cover details about different ways to Increase Maximum Execution Time in PHP Script?, Increase PHP Script Max Execution Time Limit Using ini_set_limit(), Increasing Your PHP Max Execution Time, How to increase maximum execution time in php? etc.
In PHP, the default time or maximum time taken for executing a PHP program is 30 seconds. This default time for execution can be change from php.ini Configuration file of php. The directive max_execution_time in php.ini sets the maximum time in seconds a PHP script is allowed to run before it is terminated by PHP parser.
How to Increase Maximum Execution Time in PHP Script?
This PHP tutorial covers all the topics of How to Increase Maximum Execution Time in PHP Script? using different ways.
1. What is Default time for Executing PHP Script?
2. Need of Increasing Your PHP Max Execution Time
3. What are 4 Ways to Change PHP program's Max Execution Time?
4. Increasing Your PHP Max Execution Time
Let's start PHP tutorial with questions, What is Default time for Executing PHP Script?
What is Default time for Executing PHP Script?
In PHP, the default time for executing a PHP program is 30 seconds. This default time for execution can be change from php.ini Configuration file of php. The directive max_execution_time in php.ini sets the maximum time in seconds a PHP script is allowed to run before it is terminated by PHP parser.
 |
Increasing Your PHP Max Execution Time |
The Directive max_execution_time useful for preventing scripts that try to keep server more busy.When running PHP from the command line the default setting is 0. If it is 0, means that there PHP Script can run or Execute for an infinite time limit.
IMPORTANT: The maximum execution time is not affected by system calls, stream operations.
On other hand, web servers may consist different timeout configurations or maximum Execution time, that may also interrupt or halt PHP execution. Apache Web Server has a Timeout directive and IIS(Internet Information Services) has a CGI timeout function. Both default to 300 seconds.
Need of Increasing Your PHP Max Execution Time
As now we know, the default time for executing a PHP program is 30 seconds. so the user will get errors when the PHP script will exceeds maximum time limit. The PHP Script may require longer time than default time when script is performing some heavy import or export operation of database files.
Another scenario, like Where PHP Script has to bulk emails. To avoid the termination of Script before completing task, you need to increase the execution time limit by changing max_execution_time Directive value from php.ini file.
What are Ways to Change PHP program Execution Time?
There are following ways to change the value of max_execution_time directive that is set with the php.ini configuration file of PHP.
1. By Setting max_execution_time directive value
2. By using set_time_limit() function
3. By using ini_set() function
4. By Setting php_value command in .htaccess file
Now lets look at each way in detail.
1. By Setting max_execution_time directive value
You can change the max_execution_time directive in php.ini configuration file of PHP. Open php.ini file in any editor like Sublime text and search for max_execution_time directive. After finding it,you need to change the value of it, as required by the PHP script. The Value should be seconds.
For Example, Look below lines from php.ini which sets max_execution_time value to 1000 seconds.
; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 1000
2. By using set_time_limit() function
The set_time_limit() function is used to set the time limit for which PHP Script will be allowed to Execute or run. If this is exceeded or reached, the PHP script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
This function requires the maximum execution time, in seconds as Parameter. If set to zero, no time limit will be forced/imposed.The set_time_limit() function returns TRUE on success, or FALSE on failure.
Here, set_time_limit() restarts the timeout counter from zero. It means that, if the timeout is the default 60 seconds, and 30 seconds into script execution a call such as set_time_limit(30) is made, the script will run for a total of 60 seconds before timing out.
Now let's look at following Example, which demonstrate use of set_time_limit() function.
<?php
// The program will be executed for 5 Minutes.
ini_set('max_execution_time', 300);
?>
3. By using ini_set() function
You can also change default execution time of PHP Script using ini_set() function. This way is also similar to set_time_limit() function which also set execution time limit to PHP program. This function accepts two Parameters, the first parameter is the directive name like max_execution_time and the second parameter is the value like 300 Seconds.
<?php
ini_set(“max_execution_time”,300);
?>
The above example will set max_execution_time to 300 seconds.
IMPORTANT: If the PHP Script is Running in Safe mode, then set_time_limit() and ini_set() will not generate the required effect.
4. By Setting php_value command in .htaccess file The last way to change maximum execution time of php script is to modify .htaccess file by using the php_value command. While using PHP as an Apache module, you can modify the configuration settings using directives in Apache configuration files (e.g. httpd.conf) and .htaccess files.
There are different directives are available like PHP_INI_ALL, PHP_INI_PERDIR, or PHP_INI_SYSTEM. The php_value Command Sets the value of the specified directive. Lets look at following example:
php_value max_execution_time 300
The above statement must be written in .htaccess file.
I hope this PHP tutorial might have given a much better idea about "How to change Maximum Execution Time in PHP Script?". If you have any doubt/query related to PHP or Web Technologies, Please let me know in Comment Section. Please share it with your friends.
Sharing is caring ❤️
Thank you for Visiting PHPBash.Keep Visiting for more Interesting concepts of Php language and web technologies from basics to advanced.
As well we will also share some ready-to-use, useful code snippets for beginners to kick start their web development project.
Do you want to build a modern, lightweight, responsive website and launch quickly?
I help to create the best websites / web applications and will be available for freelance work. contact: phpgems28@gmail.com
Comments
Post a Comment
Thank you so much.